#include "top_view.h"
#include "display_list_utils.h"
#include "model/surface_utils.h"

TopView::TopView(Rectangle viewport,GLuint axisHandle,TopModel& model)
	: View(viewport),model(model){
	dlhandle = axisHandle;
}

TopView::~TopView(){
}

void TopView::setEnv(){
	glViewport ((GLint) viewport.x1,(GLint) viewport.y1, (GLsizei) viewport.width(), (GLsizei) viewport.height());
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	gluOrtho2D(0.0f,1.0f,0.0f,1.0f);
	//gluOrtho2D(-0.10, 1.05, -0.10, 1.05);
}

void TopView::display(){
	setEnv();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//gluLookAt (0, 0, 0.5, 0, 0, 0, 0, 1, 0);
	glCallList(dlhandle);

	glDisable(GL_LIGHTING);
	//Contorno del viewport
	glColor3f(0.5f,0.5f,0.5f);
	glBegin(GL_LINE_LOOP);
		glVertex3f(0.0f,0.0f,0.0f);
		glVertex3f(0.0f,0.99f,0.0f);
		glVertex3f(0.99f,0.99f,0.0f);
		glVertex3f(0.99f,0.0f,0.0f);
	glEnd();

	glDisable(GL_LIGHTING);
	CurveModel::Points::const_iterator it = model.pointsBegin();
	glColor3f(1.0f,1.0f,1.0f);
	glPointSize(3);
	glBegin(GL_LINE_STRIP);
		unsigned int pointCount=0;
		unsigned int segment=0;
		for(;it!=model.pointsEnd();++it,pointCount++){
			if(pointCount==BSpline3::step){
				pointCount=0;
				segment++;
			}
			switch(model.getSurfaceType(segment)){
				case SurfaceUtils::CILINDER:
					glColor3f(1.0f,0.0f,0.0f);
					break;
				case SurfaceUtils::BASE:
					glColor3f(0.0f,1.0f,0.0f);
					break;
				case SurfaceUtils::NONE:
					glColor3f(0.0f,0.0f,1.0f);
					break;
			}
			glVertex2f(it->x1,it->x2);
		}


	glEnd();

	CurveModel::Points::const_iterator it2 = model.controlPointBegin();
	glPointSize(4);
	glColor3f(1.0f,0.0f,0.0f);
	glBegin(GL_POINTS);
		for(;it2!=model.controlPointEnd();++it2){
			glVertex2f(it2->x1,it2->x2);
		}
	glEnd();
	glPointSize(1);
	glEnable(GL_LIGHTING);

	glPushMatrix();
		glScalef(1/20.0f,1/20.0f,1.0f);
		glCallList(DisplayListsUtils::instance().getGridHandle());
	glPopMatrix();
}

void TopView::update(Observer* observer){
}
